# 导入tushare
import tushare as ts
from pprint import pprint
import pandas as pd
from datetime import date, timedelta, datetime

# # 获取今天的日期，并转化成相应格式
day = date.today()
today = format(day.strftime('%Y%m%d'))


class GaoQian():
    """
    1. 提取交易日
    2. 根据股票代码获取交易日的数据
    3. 计算出下影线与柱的比例数据
    4. 根据当前比例数据的获取下一日的涨幅
    5. 获取数据
    """

    def __init__(self):
        # 初始化pro接口
        self.pro = ts.pro_api('cf37ab059637ac7f5d628bdd046a9bcb2c1e34c2fa7546e6b8659225')

    def get_all_name(self):
        # 查询当前所有正常上市交易的股票列表
        data1 = self.pro.stock_basic(**{
            "ts_code": "", "name": "", "exchange": "", "market": "", "is_hs": "", "list_status": "", "limit": "",
            "offset": ""
        }, fields=["ts_code", "name", "symbol", "area", "industry", "market", ])
        str_code = ""
        for idx, data in data1.iterrows():
            # print("[{}]: 股票名称：{},股票代码：{},所属行业：{}".format(idx, data[2], data[1], data[4]))
            str_code = str_code + data[1] + ","
            if idx > 100:
                return str_code

    def get_name(self):
        """ 获取股票名称 """
        pass

    def get_next_date(self, last_date):
        """ 根据日期获取下一个交易日的增幅"""
        df = self.pro.trade_cal(
            **{"exchange": "", "cal_date": "", "start_date": 20220408, "end_date": "", "is_open": 1, "limit": "",
               "offset": 1
               }, fields=["exchange", "cal_date", "is_open", "pretrade_date"
                          ])
        for idx, data in df.iterrows():
            # 拉取数据
            pct_chg = self.pro.daily(
                **{"ts_code": "000998.SZ", "trade_date": data[1], "start_date": "", "end_date": "", "offset": "",
                   "limit": ""
                   }, fields=[
                    "pct_chg"
                ])
            print("下一日涨跌幅：", pct_chg)
            return pct_chg


def jisuan(open, high, low, close, trade_date, ts_code):
    """ 计算下影线 / 柱体 大于2.5"""
    shiti = max(open, close) - min(open, close)
    xiaying = min(open, close) - low
    if shiti == 0.0 or xiaying == 0.0:
        print("跳过：", shiti, xiaying)
        # pass
    else:
        jieguo = round(xiaying / shiti, 2)
        if jieguo > 2.5:
            # print(jieguo)
            # name = get_name(ts_code)
            print(open, high, low, close, trade_date, ts_code)
            return trade_date

    def get_data(self):
        """ 获取每只股票的当天数据 """
        # 拉取数据
        df = self.pro.daily(**{
            "ts_code": "000998.SZ", "trade_date": "", "start_date": "20220406", "end_date": "", "offset": "",
            "limit": ""
        }, fields=["ts_code", "trade_date", "open", "high", "low", "close", "pre_close", "change", "pct_chg", "vol",
                   "amount"
                   ])

        for idx, data in df.iterrows():
            # 提取数据
            print(data[2], data[3], data[4], data[5], data[1])
            jisuan(data[2], data[3], data[4], data[5], data[1], data[0])

# # 获取今天的日期，并转化成相应格式
# day = date.today()
# day = format(day.strftime('%Y%m%d'))
#
# # 利用query接口可直接获取一段时间的交易日，is_open指是否开盘，1即开盘，就是交易日
# data = pro.query('trade_cal', start_date='20220401', end_date=day, is_open='1')
# # 返回数据集中cal_date字段即交易日
# trade_days = list(data['cal_date'])
